﻿using CIS.Client.Common.HttpRequestHelper;
using CIS.Client.Modules.Operation.Models;
using CIS.Common.Dtos.ClientDtos;
using CIS.Common.Dtos.CommandDtos;
using CIS.Common.Entitys;
using CIS.Common.Enums;
using Newtonsoft.Json;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace CIS.Client.Modules.Operation.ViewModels
{
    public class DownLoadViewModel : BindableBase, IDialogAware
    {
        private readonly HttpRequestClient _httpRequest;

        public DownLoadViewModel(HttpRequestClient httpRequestClient)
        {
            JobList = new List<JobModel>();
            this._httpRequest = httpRequestClient;
        }
        #region 属性
        private List<JobModel>? jobList;

        public List<JobModel>? JobList
        {
            get { return jobList; }
            set { jobList = value; }
        }

        private bool downLoadBtnEnable = true;

        public bool DownLoadBtnEnable
        {
            get { return downLoadBtnEnable; }
            set { downLoadBtnEnable = value; RaisePropertyChanged(nameof(DownLoadBtnEnable)); }
        }
        #endregion

        #region 命令
        private DelegateCommand downLoadCommand;
        /// <summary>
        /// DownLoad 按钮命令
        /// </summary>
        public DelegateCommand DownLoadCommand
        {
            get { return downLoadCommand ?? new DelegateCommand(DownLoadFunction); }
        }

        private DelegateCommand cancelCommand;


        /// <summary>
        /// Cancel 按钮命令
        /// </summary>
        public DelegateCommand CancelCommand
        {
            get { return cancelCommand ?? new DelegateCommand(CancelFunction); }
        }
        #endregion

        #region 方法
        /// <summary>
        /// DownLoad 按钮命令执行逻辑
        /// </summary>
        private async void DownLoadFunction()
        {
            ConfirmFunction();
        }

        /// <summary>
        /// Cancel 按钮命令执行逻辑
        /// </summary>
        private void ConfirmFunction()
        {
            DialogResult dialogResult = new DialogResult(ButtonResult.OK);
            this.RequestClose.Invoke(dialogResult);
        }

        /// <summary>
        /// Cancel 按钮命令执行逻辑
        /// </summary>
        private void CancelFunction()
        {
            DialogResult dialogResult = new DialogResult(ButtonResult.Cancel);
            this.RequestClose.Invoke(dialogResult);
        }
        #endregion



        #region IDialogAware 属性
        public string Title { get; set; } = "DownLoad";

        public event Action<IDialogResult>? RequestClose;

        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {

        }

        public void OnDialogOpened(IDialogParameters parameters)
        {
            var paras = parameters.GetValue<List<JobModel>>("Maps");
            if (paras !=null)
            {
                JobList = paras;    
                if(JobList.Count ==0)
                    DownLoadBtnEnable = false;
            }            
        }
        #endregion
    }
}
